# jsb2/plugs/remotecallbacks.py
#
#

""" dispatch remote events. """

## jsb2 imports

from jsb2.utils.lazydict import LazyDict
from jsb2.utils.generic import fromenc
from jsb2.utils.exception import handle_exception
from jsb2.lib.callbacks import callbacks, remote_callbacks, first_callbacks
from jsb2.lib.container import Container
from jsb2.lib.eventbase import EventBase
from jsb2.lib.errors import NoProperDigest
from jsb2.lib.commands import cmnds
from jsb2.lib.examples import examples
from jsb2.contrib.xmlstream import NodeBuilder, XMLescape, XMLunescape
from jsb2.contrib.simplejson import loads, dumps

## basic imports

import logging
import copy
import hmac
import hashlib
import cgi

## defines

cpy = copy.deepcopy

## callback

def remotecb(bot, event):
    """ dispatch an event. """
    try: container = Container().load(event.txt)
    except TypeError:
        handle_exception()
        logging.warn("remotecallbacks - not a remote event - %s " % event.userhost)
        return
    logging.debug('doing REMOTE callback')
    try:
        digest = hmac.new(str(container.hashkey), XMLunescape(container.payload), hashlib.sha512).hexdigest()
        logging.debug("remotecallbacks - digest is %s" % digest)
    except TypeError:
        handle_exception()
        logging.error("remotecallbacks - can't load payload - %s" % container.payload)
        return
    if container.digest == digest: e = EventBase().load(XMLunescape(container.payload))
    else: raise NoProperDigest()
    e.txt = XMLunescape(e.txt)
    e.nodispatch = True
    e.forwarded = True
    bot.doevent(e)
    event.status = "done"  
    return

remote_callbacks.add("MESSAGE", remotecb)
	